var mongoose = require('mongoose'),
    Schema = mongoose.Schema,
    properties = require('../conf/properties'),
    baseDao = require('./baseDao');

var featureSchema = new Schema(
	{
    	type : {
	    	type: String, 
	    	enum: ['Feature'], 
	    	default: 'Feature'
	    },
      	properties : {},
      	geometry : {  //Un feature contiene un objeto geométrico
        	type : {
		    	type: String, 
		    	enum: ['Point', 'LineString', 'Polygon', 'Multipoint', 'MultiLineString'], 
		    	default: 'Polygon'
		    },
         	coordinates: {}
      	}
  }	
);

//Esquema JSON para las locaciones geográficas
var geoSchema = new Schema(
		{
		    type : {
		    	type: String, 
		    	enum: ['FeatureCollection'], 
		    	default: 'FeatureCollection'
		    },
		    features : [featureSchema],
		    id : { type: Number, unique: true, required: true },
		    geospatial: {
	      		point:[Number],
	      		multipoint: [
	      			{ point: [Number] }
	      		]
	      	}
		}
);

geoSchema.index({'geospatial.point': '2d'});
geoSchema.index({'geospatial.multipoint.point': '2d'});

//Conecto con la DB y seteo el Model
mongoose.connect(properties.mongodbConnectionURI);
var geos = mongoose.model('geos', geoSchema);

module.exports.getAll = function (short, callback) {
	baseDao.getAll(geos, short, '["id"]', callback);
}

module.exports.find = function (filters, callback) {
	baseDao.find(geos, filters, false, callback);
}

module.exports.findOne = function (filters, callback) {
	baseDao.find(geos, filters, true, callback);
}

module.exports.set = function (geo, callback) {
	baseDao.set(geos, geo, callback);
}

module.exports.update = function (id, data, callback) {
	baseDao.update(geos, 'id', id, data, callback);
}

module.exports.upsert = function (id, data, callback) {
	baseDao.upsert(geos, 'id', id, data, callback);
}

module.exports.remove = function (id, callback) {
	baseDao.remove(geos, 'id', id, callback);
}